package libofei.common.utils;

import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyUtil {

    private static Properties props;


    static {
        loadProps();
    }

    synchronized static private void loadProps() {

        props = new Properties();
        InputStream in = null;
        try {
            in = PropertyUtil.class.getClassLoader().getResourceAsStream("application.properties");
            props.load(in);
            String name = getProperty("spring.profiles.active");
            if (name.equals("dev")) {
                in = PropertyUtil.class.getClassLoader().getResourceAsStream("application-dev.yml");
            } else if (name.equals("prod")) {
                in = PropertyUtil.class.getClassLoader().getResourceAsStream("application-prod.yml");
            } else if(name.equals("test")){
                in = PropertyUtil.class.getClassLoader().getResourceAsStream("application-test.yml");
            }
            props.load(in);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {

            }
        }
    }

    public static String getProperty(String key) {
        if (null == props) {
            loadProps();
        }
        return props.getProperty(key);
    }
}
